/*
 * bluetooth.c
 *
 *  Created on: 2012.11.27.
 *      Author: Peter
 */

#include "stm32f4xx.h"
#include "stm32f4xx_usart.h"
#include "bluetooth.h"

#define IO_E_OK 1U

uint8_t tx_buffer[50];
uint8_t in_buffer[RX_BUFFER_SIZE];
uint8_t BT_rx_packnum = 0;
uint8_t BT_Rx_Phase  = 0;
uint8_t BT_data_len = 0;
uint8_t BT_data_counter = 0;
uint8_t BT_tx_pack_part;
uint8_t BT_addr_counter;
uint8_t to_send = 0;
BluetoothBufferType rx_buffer[20];

void Init_Bluetooth(void)
{

		/* Create USART3 init stucture */
		GPIO_InitTypeDef GPIO_InitStructure;
		USART_InitTypeDef usart3_init;
		NVIC_InitTypeDef NVIC_InitStructure_UART;

		GPIO_StructInit(&GPIO_InitStructure);
		USART_StructInit(&usart3_init);


		RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
		//configure AF
		GPIO_PinAFConfig(GPIOD,GPIO_PinSource8,GPIO_AF_USART3);
		GPIO_PinAFConfig(GPIOD,GPIO_PinSource9,GPIO_AF_USART3);
		GPIO_PinAFConfig(GPIOD,GPIO_PinSource11,GPIO_AF_USART3);
		GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_USART3);
		//configure ports, &GPIO_InitStructure);

		 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

		 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
		 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		 GPIO_Init(GPIOD, &GPIO_InitStructure);


		  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
		  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
		  GPIO_Init(GPIOD, &GPIO_InitStructure);

		  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ; // CTS pin
		  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL ;
		  GPIO_Init(GPIOD, &GPIO_InitStructure); /// GPIO D

		  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12  ; // RTS pin
		  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
		  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
		  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL ;
		  GPIO_Init(GPIOD, &GPIO_InitStructure);
		/* Enable clock for USART */
		RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
		/* Set default init parameters*/

		usart3_init.USART_BaudRate = 115200;
		usart3_init.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
		usart3_init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
		usart3_init.USART_Parity = USART_Parity_No;
		usart3_init.USART_WordLength = USART_WordLength_8b;
		USART_Init(USART3,&usart3_init);

		NVIC_InitStructure_UART.NVIC_IRQChannel = USART3_IRQn;
		NVIC_InitStructure_UART.NVIC_IRQChannelPreemptionPriority = 3;     // in the RM0090 its 44
		NVIC_InitStructure_UART.NVIC_IRQChannelSubPriority = 0;                // could not find about it
		NVIC_InitStructure_UART.NVIC_IRQChannelCmd =  ENABLE;          // Here Is have disabled the interrupts from USART3 - for testing in polling mode- u can change to ENABLE

		NVIC_Init(&NVIC_InitStructure_UART);
		USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
		//USART_ITConfig(USART3,USART_IT_TXE,ENABLE);
		USART_Cmd(USART3,ENABLE);
}

void Init_BluetoothNotUsed(void)
{
	/* Create USART3 init stucture */
	GPIO_InitTypeDef GPIO_InitStructure;

	GPIO_StructInit(&GPIO_InitStructure);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	//configure AF

	//configure ports, &GPIO_InitStructure);

	 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
	 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
	 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_11 | GPIO_Pin_12;

	 GPIO_SetBits(GPIOD,GPIO_Pin_8);
	 GPIO_SetBits(GPIOD,GPIO_Pin_9);

	 GPIO_Init(GPIOD, &GPIO_InitStructure);


	  }
/**************************************************
 * Function: SendDataToBT
 * Description: Send length byte of data to BT
 *
 * */
uint8_t SendDataToBT(uint16_t data)
{
	USART_SendData(USART3,data);
	while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==0);
	return IO_E_OK;
}

void USART3_IRQHandler(void)
{
	uint8_t temp_len;

	if(USART_GetFlagStatus(USART3,USART_FLAG_RXNE) == SET)
		{
			switch(BT_Rx_Phase)
			{
			case(BT_WAITFOR_START):
				/* Waiting for start frame (0xAB)*/
				if(USART_ReceiveData(USART3) == 0xAB)
				{
					rx_buffer[BT_rx_packnum].addr = 0x00000000U;
					/* Start frame received, reception can be continued */
					BT_Rx_Phase++;

				}
				break;
			case(BT_GET_ADDR):
                /* Receive 32 byte address (ID of message) - 4 byte expected*/
				if(BT_addr_counter < 4)
				{
					/*Save address to rx. buffer */
					rx_buffer[BT_rx_packnum].addr |=  (uint32_t) (USART_ReceiveData(USART3) << (BT_addr_counter * 8));
					BT_addr_counter++;
				} else
				{
					/* Address received, step to get msg length*/
					BT_Rx_Phase++;
					BT_addr_counter = 0;
				}

			break;
			case(BT_GET_MSG_LEN):
				temp_len = (uint8_t)USART_ReceiveData(USART3);
				if(temp_len < MAX_MSG_LEN)
				{
					BT_data_len = temp_len;
					rx_buffer[BT_rx_packnum].len = temp_len;
					BT_Rx_Phase++;
				}
				else
				{
					/* If length is invalid terminate reception */
					BT_data_len = 0;
					BT_Rx_Phase = 0;
				}
				break;
			case(BT_GET_MSG_DATA):
				if(BT_data_len > 0)
				{
					rx_buffer[BT_rx_packnum].data[BT_data_counter ] = USART_ReceiveData(USART3);
					BT_data_counter++;
					BT_data_len--;
				} else
				{
					BT_Rx_Phase++;
					BT_data_counter = 0;
				}
				break;
			case(BT_GET_MSG_DATA + 1):
				{
					BT_Rx_Phase = 0;
					rx_buffer[BT_rx_packnum].valid = 1;
					BT_rx_packnum++;
				}
				break;
			}
		}
	else
	{
		if (USART_GetFlagStatus(USART3,USART_FLAG_TXE) == SET)
		{
			USART_ClearFlag(USART3,USART_FLAG_TXE);
			if(to_send > 0)
			{
				to_send--;
				USART_SendData(USART3, tx_buffer[3 - to_send]);
			}
		}
	}
}




